const mongoose = require('mongoose')
const db = `mongodb://localhost/VueDs-db`
const glob = require('glob')
const {resolve} = require('path')
exports.initSchema = () => {
  glob.sync(resolve(__dirname, './schema', '**/*.js')).forEach(require)
}
exports.connect = () => {
  // 连接数据库
  mongoose.connect(db)
  let maxConnectTimes = 0
  return new Promise((resolve, reject) => {
    // 增加数据库监听事件
    mongoose.connection.on(`disconnected`, (err) => {
      console.log('*****数据库断开')
      if (maxConnectTimes <= 3) {
        mongoose.coonect(db)
      } else {
        reject(err)
        throw new Error('数据库出现问题，请修复')
      }
    })
    // 增加数据库监听事件
    mongoose.connection.on('error', (err) => {
      console.log('*****数据库错误')
      if (maxConnectTimes <= 3) {
        mongoose.coonect(db)
      } else {
        reject(err)
        throw new Error('数据库出现问题，请修复')
      }
    })
    // 增加数据库监听事件
    mongoose.connection.once('open', () => {
      console.log(`mongodb connect successfully`)
      resolve()
    })
  })
}
